package com.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author lenovo
 * @Description
 * @Date2025-2-26 15:05
 */
public class PetStore {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
       login();
    }

    public static void login() {
        //1.获取连接
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        try (Connection connection = DBUtil.getConnection()) {

            String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet rs = statement.executeQuery();

            if (rs.next()) {
                Users user = new Users();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setAdmin(rs.getInt("admin"));
                System.out.println("登录成功！");

                if (user.getAdmin()==1) {
                    adminMenu();
                } else {
                    userMenu();
                }
            } else {
                System.out.println("用户名或密码错误！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private static void adminMenu() {
        System.out.println("\n宠物商店管理系统");
        while (true) {
            System.out.println("\n选择菜单：");
            System.out.println("1）添加宠物");
            System.out.println("2）更新宠物");
            System.out.println("3）删除宠物");
            System.out.println("4）查看宠物");
            System.out.println("5）购买宠物");
            System.out.print("请输入你的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1: addPet();break;
                case 2: updatePet();break;
                case 3: deletePet();break;
                case 4: selectPet();break;
                case 5: buyPet();break;
            }
        }
    }
    private static void userMenu() {
        System.out.println("\n宠物商店管理系统");
        while (true) {
            System.out.println("\n选择菜单：");
            System.out.println("1）查看宠物");
            System.out.println("2）购买宠物");
            System.out.print("请输入你的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1: selectPet();break;
                case 2: buyPet();break;
            }
        }
    }

    public static void addPet(){
        System.out.println("请输入主人id");
        Integer users_id = scanner.nextInt();

        System.out.println("请输入宠物昵称");
        String petname = scanner.nextLine();
        scanner.nextLine();

        System.out.println("请输入宠物类型");
        String type = scanner.nextLine();

        System.out.println("请输入宠物年龄");
        Integer age = scanner.nextInt();

        System.out.println("请输入宠物性别");
        String gender = scanner.nextLine();

        try (Connection conn = DBUtil.getConnection()) {
            String sql = "INSERT INTO pets (users_id,petname,type, age, gender) VALUES (?, ?, ?, ?,?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, users_id);
            stmt.setString(2, petname);
            stmt.setString(3, type);
            stmt.setInt(4, age);
            stmt.setString(5, gender);
            stmt.executeUpdate();
            System.out.println("宠物添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // 更新宠物
    public static void updatePet() {
        System.out.print("请输入要更新的宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入新的宠物昵称: ");
        String petName = scanner.nextLine();
        System.out.print("请输入新的宠物类型: ");
        String type = scanner.nextLine();
        System.out.print("请输入新的宠物年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入新的宠物性别: ");
        String gender = scanner.nextLine();

        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE pets SET petname = ?, type = ?, age = ?, gender = ? WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, petName);
            stmt.setString(2, type);
            stmt.setInt(3, age);
            stmt.setString(4, gender);
            stmt.setInt(5, id);
            int rows = stmt.executeUpdate();
            if (rows > 0) {
                System.out.println("宠物更新成功！");
            } else {
                System.out.println("未找到该宠物！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void deletePet() {
        System.out.print("请输入要删除的宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM pets WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            int rows = stmt.executeUpdate();
            if (rows > 0) {
                System.out.println("宠物删除成功！");
            } else {
                System.out.println("未找到该宠物！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查看所有宠物
    public static void selectPet() {
        System.out.println("\n选择排序方式:");
        System.out.println("1. 按年龄");
        System.out.println("2. 按类型");
        System.out.print("请选择: ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql = "SELECT * FROM pets";
        if (choice == 1) {
            sql += " ORDER BY age";
        } else if (choice == 2) {
            sql += " ORDER BY type";
        }

        try (Connection conn = DBUtil.getConnection()) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") +
                        ", 昵称: " + rs.getString("petname") +
                        ", 类型: " + rs.getString("type") +
                        ", 年龄: " + rs.getInt("age") +
                        ", 性别: " + rs.getString("gender"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 购买宠物
    public static void buyPet() {
        System.out.print("请输入要购买的宠物ID: ");
        int petId = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入您的用户ID: ");
        int userId = scanner.nextInt();
        scanner.nextLine();

        try (Connection conn = DBUtil.getConnection()) {
            // 检查宠物是否存在
            String checkPetSql = "SELECT * FROM pets WHERE id = ? AND users_id IS NULL";
            PreparedStatement checkPetStmt = conn.prepareStatement(checkPetSql);
            checkPetStmt.setInt(1, petId);
            ResultSet rs = checkPetStmt.executeQuery();

            if (rs.next()) {
                // 检查用户积分是否足够
                String checkUserSql = "SELECT points FROM users WHERE id = ?";
                PreparedStatement checkUserStmt = conn.prepareStatement(checkUserSql);
                checkUserStmt.setInt(1, userId);
                ResultSet userRs = checkUserStmt.executeQuery();

                if (userRs.next()) {
                    int userPoints = userRs.getInt("points");
                    if (userPoints >= 10) {
                        // 更新宠物的主人ID
                        String updatePetSql = "UPDATE pets SET users_id = ? WHERE id = ?";
                        PreparedStatement updatePetStmt = conn.prepareStatement(updatePetSql);
                        updatePetStmt.setInt(1, userId);
                        updatePetStmt.setInt(2, petId);
                        updatePetStmt.executeUpdate();

                        // 扣除用户积分
                        String updateUserSql = "UPDATE users SET points = points - 10 WHERE id = ?";
                        PreparedStatement updateUserStmt = conn.prepareStatement(updateUserSql);
                        updateUserStmt.setInt(1, userId);
                        updateUserStmt.executeUpdate();

                        System.out.println("购买成功！");
                    } else {
                        System.out.println("积分不足，无法购买！");
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}

